Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
31 lines (28 loc) · 911 Bytes

3.8.3 - Coroutine/MySQL->query.md

File metadata and controls

31 lines (28 loc) · 911 Bytes

Coroutine\MySQL->query

执行SQL语句

array|bool query(string $sql, double $timeout = -1)
  • $sql:SQL语句
  • $timeout:超时时间,$timeout如果小于或等于0,表示永不超时。在规定的时间内MySQL服务器未能返回数据,底层将返回false,设置错误码为110,并切断连接
  • 返回值:超时/出错返回false,否则以数组形式返回查询结果
$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect([
    'host' => '127.0.0.1',
    'port' => 3306,
    'user' => 'user',
    'password' => 'pass',
    'database' => 'test',
]);
$res = $swoole_mysql->query('select * from $table');
if($res === false) {
	return;
}
foreach ($res as $value) {
	echo $value['f_filed_name'];
}

延迟接收

设置defer后,调用query会直接返回true。调用recv才会进入协程等待,返回查询的结果。